(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:rolelist
     * @description
     * # rolelist 
     */
    angular.module('webApp')

    .directive('exportData', ['dataService', '$compile', function(dataService, $compile) {
        return {
            restrict: 'E',
            // require: 'ngModel',
            replace: true,
            scope: {
                // ngModel: '=',
                startcreated: '=',
                endcreated: '=',
                orderid: '=',
                chosedstatus: '=',
                userId: '=',
                giftId: '=',
                gifttype: '=',
                appid: '=',
                formType: '=',
                formId: '=',
                type: "@",
                memberid: '=',
                pickpointid: '='
            },
            template: "<span><a href='javascript:;'class='sui-btn btn-success' ng-click='getorderList(2)'> {{'导出'|translate}}</a><span class='hide' id='downorder'></span></span>",//<a href='javascript:;'class='sui-btn btn-success' ng-click='getorderList(1)'> {{'导出'|translate}}</a>
            // templateUrl: "views/directives.importexcel.html",
            controller: ['$templateCache', '$rootScope', '$scope', '$timeout', 'sku', 'helper', 'ERP_CONFIG', '$window', 'sessionService', '$filter', function($templateCache, $rootScope, $scope, $timeout, sku, helper, ERP_CONFIG, $window, sessionService, $filter) {

                $scope.getorderList = function(type) {
                    if ($scope.type == 3) {
                        if (!$scope.startcreated) {
                            helper.myAlert("开始时间不能为空");
                            return;
                        }
                        if (!$scope.endcreated) {
                            helper.myAlert("结束时间不能为空");
                            return;
                        }
                    }

                    postExportcsv(type);
                }

                function postExportcsv(type) {
                    console.error("$scope.formID", $scope.formID);
                    var token = sessionService.get('token');
                    var query = {
                        store_id: $rootScope.store_id,
                        type: $scope.type,
                        "timezone": window.localStorage.timezone,
                    };
                    if ($scope.startcreated) {
                        query.start_created = toStdTime($scope.startcreated);
                    }
                    if ($scope.endcreated) {
                        query.end_created = toStdTime($scope.endcreated);
                    }
                    if ($scope.giftId) {
                        query.gift_id = $scope.giftId;
                    }
                    if ($scope.appid) {
                        query.appid = $scope.appid;
                    }
                    if ($scope.chosedstatus) {
                        query.status = $scope.chosedstatus;
                    }
                    if ($scope.userId) {
                        query.user_id = $scope.userId;
                    }
                    if ($scope.gifttype) {
                        query.gifttype = $scope.gifttype;
                    }
                    if ($scope.user_id) {
                        query.user_id = $scope.user_id;
                    }
                    if ($scope.formType) {
                        query.form_type = $scope.formType;
                    }
                    if ($scope.formId) {
                        query.form_id = $scope.formId;
                    }
                    if ($scope.chosedstatus == 'weishenhe') {
                        query.status = 1;
                    }
                    if ($scope.chosedstatus == 'daijiesuan') {
                        query.status = 2;
                    }
                    if ($scope.chosedstatus == 'yijiesuan') {
                        query.status = 3;
                    }
                    if ($scope.orderid) {
                        query.order_id = $scope.orderid;
                    }
                    if ($scope.memberid) {
                        query.member_id = $scope.memberid;
                    }
                    if ($scope.pickpointid) {
                        query.pickpoint_id = $scope.pickpointid;
                    }
                    console.error(query);
                    var con = JSON.stringify(query);
                    if(type == 2)
                    {
                        $("#downorder").removeClass("hide").html("导出中");
                        dataService.exportcsv(con,"dhf.exportcsv.downorder").then(function(result) {
                            if(result.filename)
                            {
                                //定时请求
                                intval(result.filename);
                            }
                            else
                            {
                                $("#downorder").html("导出失败");
                            }
                        });
                    }
                    else if(type == 1)
                    {
                        var url2 =ERP_CONFIG.api2Url + '/api2/rest/?token=' + token + '&content=' + con+'&method=dhf.exportcsv.order';
                        // var url = ERP_CONFIG.eprUrl + '/rest/v1/php/dhf/exportcsv/order/add?token=' + token + '&content=' + con;
                        $window.open(url2, 'windowName', "_blank", 'width=200,height=100');
                    }
                }

                function intval(filename)
                {
                    //10秒请求一次
                    var timeId = setInterval(function(){
                        var query = {
                            filepath:filename
                        }
                        dataService.isfile(query,"dhf.exportcsv.isfile").then(function(result) {
                            if(result.filename)
                            {
                                //清除定时任务
                                window.clearInterval(timeId);
                                $("#downorder").html("导出成功 <a href='"+result.filename+"'>点击下载</a>");
                            }
                            else
                            {

                            }
                        });
                    },10000);
                }

                //把当地变成标准时间
                function toStdTime(x) {
                    return $filter('stdTime')(x);
                }
            }],
            link: function(scope, element, attrs, ctrl) {

            }
        }
    }])
})();